<?php
/*
	员工管理
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//声明页面上方tab按钮
$tabs=array(
	array('op'=>'list','title'=>'员工列表'),
	array('op'=>'edit','title'=>'创建员工'),
);
set_html('tabs',$tabs);
$return['depots']=$this->pdo_getall('depot',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
//声明列表页
if($_RQ['op']=='list'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('clerk',$parameter,$total);
	foreach($list as &$item){
		
	}
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			//列表展示列
			array('type'=>'string','name'=>'title','title'=>'员工名称',),
			array('type'=>'select','name'=>'depot_id','title'=>'所属仓库','options'=>'depots'),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'string','name'=>'title','title'=>'员工名称','width'=>'50'),
		),
	);
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
//声明编辑页
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		//处理表单提交
		$post=get_postdata();
		//获取表单数据
		$data=array_fields(array('title','depot_id','mid'),$post);
		$id=intval($post['id']);
		if(!$id){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$this->pdo_insert('clerk',$data);
		}else{
			$this->pdo_update('clerk',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('修改成功！','','list');
	}
	//整理编辑页
	$id=intval($_RQ['id']);
	if(!$id){
		$item=array();
	}else{
		$item=$this->pdo_get('clerk',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'门店信息设置',
		'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'员工名称','must'=>true),
				array('type'=>'select','name'=>'depot_id','title'=>'所属仓库','options'=>'depots'),
				array('name'=>'mid','title'=>'绑定会员','type'=>'choose','action'=>'core/member.get_member/'),	
		),
	);
	$return['htmls']=$htmls;
	//返回前端
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_supplier_clerk',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_supplier_clerk',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}